package com.example.framework.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.framework.R;

import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:0.2℃
 * @date： 2022-03-31 01:34
 * @Description ：图片验证类
 */
public class TouchPictureView extends View {
    // 背景
    private Bitmap bgBitmap;
    // 背景画笔
    private Paint mPaintbg;

    // 空白块
    private Bitmap mNullBitmap;
    // 空白块画笔
    private Paint mPaintNull;

    // 移动方块
    private Bitmap mMoveBitmap;
    // 移动画笔
    private Paint mPaintMove;

    private int mWidth;
    private int mHeight;
    // 空白块的大小
    private int CARD_SIZE = 200;
    // 空白块的坐标
    private int LINE_W, LINE_H = 0;

    // 移动方块最表
    private int moveX = 200;
    // 允许的误差值
    private int errorMoveX = 10;
    private OnViewResultListener listener;


    public void setViewResultListener(OnViewResultListener listener) {
        this.listener = listener;
    }

    public TouchPictureView(Context context) {
        super(context);
        init();
    }

    public TouchPictureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchPictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaintbg = new Paint();
        mPaintNull = new Paint();
        mPaintMove = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制背景
        drawBg(canvas);
        // 绘制用来装载小方块的空白处
        drawNullCard(canvas);
        // 绘制移动的方块
        drawMoveCard(canvas);

    }

    /**
     * 截取空白块位置坐标的bitmap图像
     * @param canvas
     */
    private void drawMoveCard(Canvas canvas) {
        // 截取空白处位置坐标的Bitmap图像，使用Bitmap.createBitmap
        // 参数说明，(需要被裁剪的资源图片，裁剪的开始横坐标，裁剪的开始纵坐标，裁剪的长，裁剪的宽)
        mMoveBitmap = Bitmap.createBitmap(bgBitmap, LINE_W, LINE_H, CARD_SIZE, CARD_SIZE);
        // 绘制在View上
        canvas.drawBitmap(mMoveBitmap, moveX, LINE_H, mPaintMove);

    }

    /**
     * 绘制那个小小的空白块
     * @param canvas
     */
    private void drawNullCard(Canvas canvas) {
        // 1. 获取图片
        mNullBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_null_card);
        // 2. 计算值
        CARD_SIZE = mNullBitmap.getWidth();
        // 2.1 计算坐标
        LINE_W = mWidth / 3 * 2;
        LINE_H = mHeight / 2 - (CARD_SIZE / 2);
        // 3. 绘制
        canvas.drawBitmap(mNullBitmap, LINE_W, LINE_H, mPaintNull);
    }

    /**
     * 绘制背景
     * @param canvas
     */
    private void drawBg(Canvas canvas) {
        // 1. 获取图片
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_bg);
        // 2. 创建一个空的Bitmap
        bgBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        // 3. 将图片绘制到空的Bitmap
        Canvas bgCanvas = new Canvas(bgBitmap);
        bgCanvas.drawBitmap(mBitmap, null, new Rect(0, 0, mWidth, mHeight), mPaintbg);
        // 4. 将bgBitmap绘制到View
        canvas.drawBitmap(bgBitmap, null, new Rect(0, 0, mWidth, mHeight), mPaintbg);
    }

    /**
     * 验证码滑动事件
     * @param event
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                // 防止越界
                if (event.getX() > 0 && event.getX() < (mWidth - CARD_SIZE)) {
                    moveX = (int) event.getX();
//                    // 验证结果
//                    if (moveX < (LINE_W + errorMoveX) || moveX > (moveX - errorMoveX)) {
//                        if (listener != null) {
//                            listener.onResult();
//                            moveX = 200;
//                        }
//                    }
//                    // jdk自带的刷新事件
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                /**
                 * 抬起之后做验证
                 * moveX = LINE_W
                 */
                if (moveX < (LINE_W + errorMoveX) || moveX > (moveX - errorMoveX)) {

                    if (listener != null) {
                        listener.onResult();
                        moveX = 200;
                        invalidate();
                    }
                }

                break;
        }
        return true;
    }

    public interface OnViewResultListener {
        void onResult();
    }
}
